//
//  UIButton+EdgeInsetsClick.m
//  
//
//  Created by cheng on 2020/6/16.
//

#import "UIButton+EdgeInsetsClick.h"

@implementation UIButton (EdgeInsetsClick)

-(void)setBlock:(void(^)(UIButton*))block{
    objc_setAssociatedObject(self,@selector(block), block,OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}

-(void(^)(UIButton*))block{
    return objc_getAssociatedObject(self,@selector(block));
}
#pragma mark block回调
-(void)addTapBlock:(void(^)(UIButton*))block{
    self.block= block;
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}

-(void)click:(UIButton*)btn{
    if(self.block) {
        self.block(btn);
    }
}

// 引入间隙 x的变量 方便可变处理 button 图片以及 title的 布局
#pragma mark // 上图片下文字 x为间距
- (void)edgeInsetsImageTopWith:(CGFloat)x{
    
    self.titleEdgeInsets = UIEdgeInsetsMake(self.imageView.bounds.size.height+x, - self.imageView.bounds.size.width, 0, 0);
    self.imageEdgeInsets = UIEdgeInsetsMake(.0, 0, self.titleLabel.bounds.size.height+x, - self.titleLabel.bounds.size.width);
}
#pragma mark // 上文字下图片 x为间距
- (void)edgeInsetsImageBottomWith:(CGFloat)x{
    // 上文字下图片
    self.titleEdgeInsets = UIEdgeInsetsMake(.0, - self.imageView.bounds.size.width, self.imageView.bounds.size.height+x,  0);
    self.imageEdgeInsets = UIEdgeInsetsMake(self.titleLabel.height+x, 0, 0, - self.titleLabel.bounds.size.width);
}

#pragma mark // 默认就是 左图片右文字 x为间距
- (void)edgeInsetsImageLeftWith:(CGFloat)x{
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0, x, 0, 0);
    self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, x);
}
#pragma mark // 左文字右图片 x为间距
- (void)edgeInsetsImageRightWith:(CGFloat)x{
    
    self.titleEdgeInsets = UIEdgeInsetsMake(.0, - self.imageView.bounds.size.width, .0, self.imageView.bounds.size.width+x);
    self.imageEdgeInsets = UIEdgeInsetsMake(.0, x+self.titleLabel.bounds.size.width, .0, - self.titleLabel.bounds.size.width);
}

@end
